package com.aube.guide.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.aube.R;
import com.aube.guide.OnGuidePerformedListener;
import com.aube.guide.RectBorder;
import com.aube.guide.ViewTarget;

/**
 * Created by huyaonan on 16/6/22.
 */
public class VRGestureGuideView extends BaseGuideView {

    /**
     * rectShape focus on target
     * and clear rect to focus
     */
    private RectBorder rectShape;

    /**
     * Info dialog view
     */
    private View infoView;

    /**
     * Info ImageView
     */
    private ImageView imageView;

    public VRGestureGuideView(Context context) {
        this(context, null);
    }

    public VRGestureGuideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VRGestureGuideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        View layoutInfo = LayoutInflater.from(getContext()).inflate(R.layout.guide_layout, null);

        infoView = layoutInfo.findViewById(R.id.guide_layout);
        imageView = (ImageView) layoutInfo.findViewById(R.id.guide_imageview);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rectShape.reCalculateAll();
                if (rectShape != null && rectShape.getPoint().y != 0 && !isLayoutCompleted) {
                    if (isInfoEnabled)
                        setInfoLayout();
                    removeOnGlobalLayoutListener(VRGestureGuideView.this, this);
                }
            }
        });
    }

    @Override
    protected void drawGuide(Canvas canvas) {

    }

    @Override
    protected void showInfo() {
        if (infoView.getParent() != null)
            ((ViewGroup) infoView.getParent()).removeView(infoView);

        Rect rect = rectShape.getRect();

        int width = rect.right - rect.left;
        int height = rect.bottom - rect.top;

        LayoutParams infoDialogParams = new LayoutParams(width, height);
        int marginTop = rect.top;
        int marginLeft = rect.left;
        infoDialogParams.setMargins(marginLeft, marginTop, 0, 0);

        infoView.setLayoutParams(infoDialogParams);
        infoView.postInvalidate();

        addView(infoView);

        imageView.setImageResource(imageRes);

        infoView.setVisibility(VISIBLE);
    }

    private void setRect(RectBorder rectShape) {
        this.rectShape = rectShape;
    }

    /**
     * Builder Class
     */
    public static class Builder {

        private VRGestureGuideView vrGuideView;

        private Activity activity;

        public Builder(Activity activity) {
            this.activity = activity;
            vrGuideView = new VRGestureGuideView(activity);
        }

        public Builder setMaskColor(int maskColor) {
            vrGuideView.setMaskColor(maskColor);
            return this;
        }

        public Builder setDelayMillis(int delayMillis) {
            vrGuideView.setDelay(delayMillis);
            return this;
        }

        public Builder enableFadeAnimation(boolean isFadeAnimationEnabled) {
            vrGuideView.enableFadeAnimation(isFadeAnimationEnabled);
            return this;
        }

        public Builder setTarget(View view) {
            vrGuideView.setTarget(new ViewTarget(view));
            return this;
        }

        public Builder setTargetPadding(int padding) {
            vrGuideView.setPadding(padding);
            return this;
        }

        public Builder dismissOnTouch(boolean dismissOnTouch) {
            vrGuideView.setDismissOnTouch(dismissOnTouch);
            return this;
        }

        public Builder setUsageId(String vrGuideViewId) {
            vrGuideView.setUsageId(vrGuideViewId);
            return this;
        }

        public Builder setImageRes(int imageRes) {
            if(imageRes != 0) {
                vrGuideView.enableInfoDialog(true);
                vrGuideView.setImageRes(imageRes);
            }
            return this;
        }

        public Builder setListener(OnGuidePerformedListener mPerformedListener) {
            vrGuideView.setListener(mPerformedListener);
            return this;
        }

        public Builder performClick(boolean isPerformClick){
            vrGuideView.setPerformClick(isPerformClick);
            return this;
        }

        public Builder setCanAudoDismiss(int time) {
            vrGuideView.setCanAudoDismiss(time);
            return this;
        }

        public VRGestureGuideView build() {
            int color = activity.getResources().getColor(R.color.theme);
            RectBorder rect = new RectBorder(
                    vrGuideView.targetView,
                    vrGuideView.padding, color);
            vrGuideView.setRect(rect);
            return vrGuideView;
        }

        public VRGestureGuideView show() {
            build().show(activity);
            return vrGuideView;
        }

    }
    
}
